<template>
  <el-dialog title="设置角色" :visible="showDialog" @close="closeFn">
    <!-- lable和checkList双向绑定在一起 -->
    <template>
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          <!-- 插值表达式的值是渲染在复选框后面的，如何没有写插值表达式，则使用label属性的值渲染在复选框后面 -->
          {{ item.title }}
        </el-checkbox>

      </el-checkbox-group>
    </template>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="comfirmFn">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/model/role'
import { getEmployeeById, editEmployee } from '@/api/model/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checkList: [],
      roleList: []
    }
  },
  async created() {
    const res = await getRoleList()
    this.roleList = res.data
    console.log(this.roleList)
  },
  methods: {
    // 获取默认角色
    async getEmployeeById(id) {
      this.userId = id // 保存用户id
      const res = await getEmployeeById(id)
      // console.log(res)
      // 保存并回显角色
      this.checkList = res.data.roles.map((item) => {
        return item.id
      })
      // console.log(64, this.checkList)
    },
    // 确定按钮
    async comfirmFn() {
      await editEmployee({
        id: this.userId,
        roles: this.checkList
      })
      // 关闭弹窗
      this.closeFn()
    },
    closeFn() {
      this.$emit('update:showDialog', false)
    }
  }
}

</script>

<style>

</style>
